package mode

import (
	"service/utils"
	"time"

	"gorm.io/gorm"
)

type Lz_user struct {
	gorm.Model
	User            string     `gorm:"size:64;not null" json:"user"`
	Password        string     `gorm:"size:64;not null" json:"password"`
	Gender          int        `gorm:"type:int;not null" json:"gender"`
	Last_Login_Ip   string     `gorm:"size:64;not null" json:"last_login_ip"`
	User_Level_Id   int        `gorm:"type:int;not null" json:"user_level_id"`
	Nickname        string     `gorm:"size:128" json:"nickname"`
	Mobile          string     `gorm:"size:11" json:"mobile"`
	Avatar          string     `gorm:"size:256" json:"avatar"`
	Register_Ip     string     `gorm:"size:64" json:"register_ip"`
	Birthday        time.Time  `json:"birthday"`
	Last_Login_Time time.Time  `json:"last_login_time" gorm:"allow_null:true;default:null;"`
	Roles           []Db_roles `json:"-" gorm:"many2many:Users_Roles;joinForeignKey:User_Id;joinReferences:Role_Id"`
	Apm             []Db_apm   `gorm:"many2many:db_user_apm;"` // 添加一对多关系
}

type IresUserList struct {
	gorm.Model
	User            string     `gorm:"size:64;not null" json:"user"`
	Gender          int        `gorm:"type:int;not null" json:"gender"`
	Last_Login_Ip   string     `gorm:"size:64;not null" json:"last_login_ip"`
	User_Level_Id   int        `gorm:"type:int;not null" json:"user_level_id"`
	Nickname        string     `gorm:"size:128" json:"nickname"`
	Mobile          string     `gorm:"size:11" json:"mobile"`
	Avatar          string     `gorm:"size:256" json:"avatar"`
	Register_Ip     string     `gorm:"size:64" json:"register_ip"`
	Birthday        time.Time  `json:"birthday"`
	Last_Login_Time time.Time  `json:"last_login_time"`
	Roles           []Db_roles `json:"roles" gorm:"many2many:Users_Roles;joinForeignKey:User_Id;joinReferences:Role_Id"`
}

func (m *Lz_user) Encrypto() error {
	ENPASSWD, err := utils.Encrypto(m.Password)
	// fmt.Printf("ENPASSWD %v", ENPASSWD)
	if err == nil {
		m.Password = ENPASSWD
	}
	return err
}

func (m *Lz_user) BeforeCreate(tx *gorm.DB) (err error) {
	return m.Encrypto()
}
